package applets;
import java.applet.Applet;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import wheel.KeyWheel;
import wheel.M209Wheels;

import algorithms.Key;


public class GetNextExternalKeyApplet extends Applet implements ActionListener{

    /**
     * serial ID
     */
    private static final long serialVersionUID = -4316982731468739714L;

    TextField inputLine = new TextField(15);
    
    M209Wheels m209wheels = new M209Wheels();
    List<KeyWheel> keyWheels;
    
    public GetNextExternalKeyApplet() {
        add(inputLine);
        inputLine.addActionListener(this);
        inputLine.setText("external key e.g. PURPLE");
        keyWheels= m209wheels.getKeyWheels();

    }
    
    public void actionPerformed(ActionEvent event) {
	
        String externalKey = inputLine.getText();
        char[] charArray = externalKey.toCharArray();
        for(int i=0;i<6;i++){
            keyWheels.get(i).setInitialPosition(Key.charToKey(charArray[i]));
        }
        for(KeyWheel wheel : keyWheels){
            wheel.setNextPosition();
        }
        String sResult = new StringBuilder().append(keyWheels.get(0).getPosition().getKey()).append(keyWheels.get(1).getPosition().getKey()).append(keyWheels.get(2).getPosition().getKey()).append(keyWheels.get(3).getPosition().getKey()).append(keyWheels.get(4).getPosition().getKey()).append(keyWheels.get(5).getPosition().getKey()).toString();
        inputLine.setText(sResult);
    } 
}
